import {defineStore} from "pinia";
import {cloneDeep} from 'lodash';
import {INTERVIEW_JSON} from "@/schema/interview";
import {IInterviewInfo, IMessage, ISettingInterview} from "@/interface/interviewInfo";
import {formatDateTime} from "@/assets/ts/utils";

interface checkMessage {
    accept: 'danger' | 'warning' | 'already',
    messages: Array<any>
}

export const useInterviewJsonStore = defineStore('interviewJson', () => {
    const interview_json = cloneDeep(INTERVIEW_JSON)
    const interviewNewStore = ref<IInterviewInfo>(interview_json)

    function AttachSetting(data: ISettingInterview) {
        interviewNewStore.value.Settings = data;
    }

    function PushMessage(data: IMessage) {
        interviewNewStore.value.Messages.push(data);
    }

    function ResetStore(){
        interviewNewStore.value = cloneDeep(INTERVIEW_JSON)
    }

    function CheckSetting() {
        let result: checkMessage =
            {accept: 'already', messages: []}

        let b1 = false, b2 = false, b3 = false;

        function pushRes(content: string, type: 'title' | 'content' | 'danger' | 'warning' | 'end' = 'content') {
            result.messages.push({type: type, message: content});
            if (type === 'danger') {
                result.accept = "danger"
            } else if (result.accept !== 'danger' && type === 'warning') {
                result.accept = "warning"
            }
        }

        function AddTitle(level: number) {
            if (level === 1 && !b1) {
                result.messages.push({type: 'title', message: '需求设置'});
                b1 = true;
            } else if (level === 2 && !b2) {
                result.messages.push({type: 'title', message: '服务设置'})
                b2 = true;
            } else if (level === 3 && !b3) {
                result.messages.push({type: 'title', message: '隐私设置'})
                b3 = true;
            }
        }

        const setting = interviewNewStore.value.Settings;
        if (setting.resumeId === -1) {
            pushRes("简历", 'title');
            pushRes('未选择简历，请返回选择！', "danger")
        }
        if (setting.company === '') {
            AddTitle(1)
            pushRes('未选择投递公司，请返回选择！', "danger")
        }
        if (setting.post === '') {
            AddTitle(1)
            pushRes('未选择投递岗位，请返回选择！', "danger")
        }
        if (!setting.interviewType) {
            AddTitle(1)
            pushRes('未选择面试类型，请返回选择！', "danger")
        }
        if (setting.requirement === '') {
            AddTitle(1)
            pushRes('未填写岗位要求/加分项，可能会影响模拟面试效果！', "warning")
        }
        if (!setting.generateReport) {
            AddTitle(2)
            pushRes('不必生成报告，您的面试记录将不会上传', "warning")
        } else if (setting.reportName === '') {
            AddTitle(2)
            pushRes(`您没有填写报告名称，已自动补充为\"新纪录_${formatDateTime(new Date())}\"`)
            interviewNewStore.value.Settings.reportName = `新纪录_${formatDateTime(new Date())}`
        }
        if ((setting.model_choice === 'universal' && setting.universal_speaker_name === '')
            || setting.model_choice === 'hoyo' && setting.hoyo_model_name === '') {
            AddTitle(2)
            pushRes('您选择的模型有误，已自动还原为通用模型')
            interviewNewStore.value.Settings.model_choice = 'universal'
            interviewNewStore.value.Settings.universal_speaker_name = 'zh-CN-XiaoxiaoNeural'
        }
        if (!setting.cameraPermit) {
            AddTitle(3)
            pushRes('您没有开启摄像头，将无法使用一些功能')
        } else if (!setting.cameraChoice) {
            AddTitle(3)
            pushRes('您没有选择摄像头设备，将使用默认设备')
        }
        if (!setting.microphonePermit) {
            AddTitle(3)
            pushRes('您没有开启麦克风，在面试期间将只能文字输入')
        } else if (!setting.microphoneChoice) {
            AddTitle(3)
            pushRes('您没有选择摄像头设备，将使用默认设备')
        }
        if (result.accept === 'warning') {
            pushRes('请查看上述警告信息，并确认您已知风险', "end")
        } else if (result.accept === 'danger') {
            pushRes('您有未完成的必填项！请查看红色字体标注并返回修改', "end")
        } else {
            pushRes('您的设置基本通过！', "end")
        }
        return result;
    }

    return {
        interviewNewStore,
        AttachSetting,
        ResetStore,
        PushMessage,
        CheckSetting
    }
})

